define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    // edit_url: 'user/user/edit',
                    del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");
            //当内容渲染完成给编辑按钮添加`data-area`属性
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".visiting_materials_edit").data("area", ["100%", "100%"]);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                searchFormTemplate: 'customformtpl',
                searchFormVisible: true,
                search:false,
                showColumns:false,
                // showExport:false,
                // commonSearch:false,
                pageSize: 10,
                showToggle: false,
                exportOptions: {
                    fileName: '用戶列表_' + Moment().format("YYYY-MM-DD"),  //导出名称
                    ignoreColumn: ['avatar','operate'], //不导出字段
                    ignoreRow: [],
                    mso:{
                        // fileFormat:        'xlsx',
                        //修复导出数字不显示为科学计数法
                        onMsoNumberFormat: function (cell, row, col) {
                            return !isNaN($(cell).text())?'\\@':'';
                        }
                    },
                    csvSeparator: "----",//文本分割
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'mobile', title: __('登記手機號'), operate: 'LIKE'},
                      
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden')}},
                        {field: 'createtime', title: __('Createtime'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter:function(value,row,index){
                            this.buttons = [
                                        {
                                            name: 'refuse',
                                            text: '禁用',
                                            classname: 'btn btn-xs btn-warning btn-magic btn-ajax',
                                            url: 'user/user/updateuser',
                                            confirm: '确认禁用嗎？',
                                            visible: function (row) {
                                                if ( Config.updateuser && row.status == 'normal' ) {
                                                    return true;
                                                }else{
                                                    return false;
                                                }
                                                //返回true时按钮显示,返回false隐藏
                                            },
                                            success: function () {
                                                $(".btn-refresh").trigger("click");
                                            },
                                            error: function () {

                                            },
                                            refresh: true,
                                        },{
                                            name: 'agree',
                                            text: '啟用',
                                            classname: 'btn btn-xs btn-success btn-ajax',
                                            url: 'user/user/updateuser',
                                            confirm: '确认啟用嗎？',
                                            visible: function (row) {
                                                if ( Config.updateuser && row.status == 'hidden' ) {
                                                    return true;
                                                }else{
                                                    return false;
                                                }
                                                //返回true时按钮显示,返回false隐藏
                                            },
                                            success: function () {
                                                $(".btn-refresh").trigger("click");
                                            },
                                            error: function () {

                                            },
                                            refresh: true,
                                        },{
                                            name: 'edits',
                                            text: '編輯',
                                            classname: 'btn btn-xs btn-success btn-magic btn-dialog edit',
                                            url: 'user/user/edit',
                                            visible: function (row) {
                                                if ( Config.updatedit ) {
                                                    return true;
                                                }else{
                                                    return false;
                                                }
                                                //返回true时按钮显示,返回false隐藏
                                            },
                                            success: function () {
                                                $(".btn-refresh").trigger("click");
                                            },
                                            error: function () {

                                            },
                                            refresh: true,
                                        },
                                        {
                                            name: 'dengji',
                                            text: '登記',
                                            classname: 'btn btn-xs btn-success btn-magic btn-dialog edit',
                                            url: 'registr_activity_user/user_list',
                                            visible: function (row) {
                                                if ( Config.dengji ) {
                                                    return true;
                                                }else{
                                                    return false;
                                                }
                                                //返回true时按钮显示,返回false隐藏
                                            },
                                            success: function () {
                                                $(".btn-refresh").trigger("click");
                                            },
                                            error: function () {

                                            },
                                            refresh: true,
                                        },
                                          {
                                            name: 'wenjuan',
                                            text: '問卷',
                                            classname: 'btn btn-xs btn-success btn-magic btn-dialog edit',
                                            url: 'question_data/user_list',
                                            visible: function (row) {
                                                if ( Config.wenjuan ) {
                                                    return true;
                                                }else{
                                                    return false;
                                                }
                                                //返回true时按钮显示,返回false隐藏
                                            },
                                            success: function () {
                                                $(".btn-refresh").trigger("click");
                                            },
                                            error: function () {

                                            },
                                            refresh: true,
                                        },
                                         {
                                            name: 'liuyan',
                                            text: '留言',
                                            classname: 'btn btn-xs btn-success btn-magic btn-dialog edit',
                                            url: 'message_board/user_list',
                                            visible: function (row) {
                                                if ( Config.liuyan ) {
                                                    return true;
                                                }else{
                                                    return false;
                                                }
                                                //返回true时按钮显示,返回false隐藏
                                            },
                                            success: function () {
                                                $(".btn-refresh").trigger("click");
                                            },
                                            error: function () {

                                            },
                                            refresh: true,
                                        },
                                         {
                                            name: 'sign',
                                            text: '義工簽到',
                                            classname: 'btn btn-xs btn-success btn-magic btn-dialog edit',
                                            url: 'activity/sign_user',
                                            visible: function (row) {
                                                if ( Config.qiandao ) {
                                                    return true;
                                                }else{
                                                    return false;
                                                }
                                                //返回true时按钮显示,返回false隐藏
                                            },
                                            success: function () {
                                                $(".btn-refresh").trigger("click");
                                            },
                                            error: function () {

                                            },
                                            refresh: true,
                                        }

                                    ];
                                return Table.api.formatter.operate.call(this,value,row,index);
                            }}
                    ]
                ]
            });

            table.on("pre-body.bs.table",function(){
                var bt=table.data("bootstrap.table");
                if (bt){
                    bt.$toolbar.find(".export").find(".icon-share").text("導出");
                    bt.$toolbar.find(".export").find(".icon-share").removeClass("glyphicon glyphicon-export icon-share");
                }
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        indexs: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/indexs/staff_id/'+Config.staff_id,
                    add_url: 'user/user/add',
                    // edit_url: 'user/user/edit',
                    // del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                searchFormVisible: true,
                search:false,
                showColumns:false,
                showExport:false,
                // commonSearch:false,
                showToggle: false,
                pageSize: 10,
                maintainSelected: true,
                columns: [
                    [
                        {field: 'state', checkbox: true,},
                        {field: 'id', title: __('Id'), sortable: true,operate:false},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'region_address', title: __('Region_address'), operate: false},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('load-success.bs.table', function () {
                // 默認選中
                $("#table").bootstrapTable("checkBy", {field:"id", values:Config.arr})
            });
            // 获取选中项
            $(document).on("click", ".btn-selected", function () {
                // data = JSON.stringify(Table.api.selecteddata(table));
                data = Table.api.selecteddata(table);
                // Layer.alert();
                Fast.api.close(data);//这里是重点
            });
        },
        // 關懷圈選擇用戶
        caring_task_user: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/caring_task_user/ids/'+Config.ids,
                    add_url: 'user/user/add',
                    // edit_url: 'user/user/edit',
                    // del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                searchFormVisible: true,
                search:false,
                showColumns:false,
                showExport:false,
                // commonSearch:false,
                showToggle: false,
                pageSize: 10,
                singleSelect: true, //是否启用单选
                // maintainSelected: true,
                columns: [
                    [
                        {field: 'state', checkbox: true,},
                        {field: 'id', title: __('Id'), sortable: true,operate:false},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'region_address', title: __('Region_address'), operate: false},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('load-success.bs.table', function () {
                // 默認選中
                $("#table").bootstrapTable("checkBy", {field:"id", values:Config.arr})
            });
            // 获取选中项
            $(document).on("click", ".btn-selected", function () {
                // data = JSON.stringify(Table.api.selecteddata(table));
                data = Table.api.selecteddata(table);
                // Layer.alert();
                Fast.api.close(data);//这里是重点
            });
        },
        staff_users: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/staff_users/ids/'+Config.ids,
                    add_url: 'user/user/add',
                    // edit_url: 'user/user/edit',
                    // del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                searchFormVisible: true,
                search:false,
                showColumns:false,
                showExport:false,
                // commonSearch:false,
                showToggle: false,
                pageSize: 10,
                maintainSelected: true,
                columns: [
                    [
                        {field: 'id', title: __('Id'), sortable: true,operate:false},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'region_address', title: __('Region_address'), operate: false},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});